<template>
  <a-spin :spinning="confirmLoading">
   
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form"  style="border: 1px solid #ccc" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24" style="margin-bottom: -25px;">
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入内容"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="margin-bottom: -25px;">
            <a-form-model-item label="控件类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlType">
              <j-dict-select-tag type="list" v-model="model.controlType" dictCode="control_type" placeholder="请选择控件类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24" style="margin-bottom: -25px;">
            <div v-if="model.controlType=='1'" >
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <a-input v-model="model.controlValue" placeholder="请输入内容"  ></a-input>
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='2'" >
            <a-form-model-item label="内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <a-textarea v-model="model.controlValue" rows="4" placeholder="请输入内容" />
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='3'" style="margin-left: 18%;">
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <div v-for="radio in controls" :key="radio.id" style="display: flex;margin-bottom: 10px;"> 
                  <a-radio ></a-radio>
                  <a-input v-model="radio.value" placeholder="请输入内容" ></a-input>
                  <a-button @click="addControl">+</a-button>
                  <a-button @click="deleteControl(radio)">-</a-button>
                </div> 
                  
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='4'" style="margin-left: 18%;">
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <div>
                <div v-for="radio in controls" :key="radio.id" style="display: flex; margin-bottom: 10px;"> 
                  <a-checkbox ></a-checkbox>
                  <a-input v-model="radio.value" placeholder="请输入内容" style="margin-left: 10px;"></a-input>
                  <a-button @click="addControl">+</a-button>
                  <a-button @click="deleteControl(radio)">-</a-button>
                </div> 
              </div>
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='5'">
            <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
                <j-image-upload fileList="" isMultiple  v-model="model.controlValue" ></j-image-upload>
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='6'">
            <!-- <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
                <j-image-upload isMultiple  v-model="model.controlValue" ></j-image-upload>
            </a-form-model-item> -->
            <a-form-model-item label="化学药剂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <j-popup
                :keys="model.controlValue"
                key-field="id"
                field="productName"
                org-fields="id,product_name,product_code,product_spec,product_class"
                dest-fields="productId,productName,productCode,productSpec,productClass"
                code="equipmentProductQuery"
                :param="popParam"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </div>
          <div v-if="model.controlType=='7'">
            <!-- <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
                <j-image-upload isMultiple  v-model="model.controlValue" ></j-image-upload>
            </a-form-model-item> -->
            <a-form-model-item label="材料" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlValue">
              <j-popup
                v-model="model.controlValue"
                field="materialName"
                org-fields="id,material_name,material_code,material_spec,material_class,location_number"
                dest-fields="materialId,materialName,materialCode,materialSpec,materialClass,locationNumber"
                code="equipmentMaterialQuery"
                :param="popParam"
                :multi="true"
                @input="popupCallback"
                />
            </a-form-model-item>
          </div>
          </a-col>
          <a-col :span="24" style="margin-bottom: -25px;">
            <!-- <a-form-model-item style="display: flex;" label="是否必填" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="controlRequired">
                <j-dict-select-tag type="radio" v-model="model.controlRequired" dictCode="yn" placeholder="请选择触发类型" /> -->
                <a-button v-show="!model.isnew" @click="reduceDetail" type="primary" style="margin-left: 80%;margin-bottom: 30px;">-</a-button>
            <!-- </a-form-model-item> -->
            
          </a-col>
          <!-- <a-col :span="12">
            <a-button  @click="addDetail" type="primary" style="margin-left: 10px; ">+</a-button>
            <a-button  @click="reduceDetail" type="primary" style="margin-left: 10px; ">-</a-button>
          </a-col> -->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue,randomUUID } from '@/utils/util'
import JCheckbox from '../../../../components/jeecg/JCheckbox.vue'

  export default {
    name: 'YksMaintenanceTemplateDetailForm',
    components: {
        JCheckbox
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
      item:{},
      detail:{},
      controlList:[{
            id:randomUUID(),
            value:'测试'
        }],
        currentEquipmentId:''
    },
    data () {
      return {
        model:{
            // controlRequired:"是否必填",
         },
         
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/dev/yksMaintenanceTemplateDetail/add",
          edit: "/dev/yksMaintenanceTemplateDetail/edit",
          queryById: "/dev/yksMaintenanceTemplateDetail/queryById"
        },
        controls:[{
            id:randomUUID(),
            value:''
        }],
        popParam:{equipmentid:''}
      }
    },
    computed: {
      
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.model=this.detail
      this.controls=this.detail.controls
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      console.log(this.model)
    },
    mounted(){

      this.popParam={equipment_id:this.currentEquipmentId}
    },
    methods: {
      // addControl(){
      //   this.$emit("addControl",this.detail)
      // },
      // deleteControl(item){
      //   this.$emit("deleteControl",this.detail,item)
      
      // },
      addControl(){
        
        if(!this.controls){
          this.controls=[]
        }
        let control={
            id:randomUUID(),
            value:''
        }
        // this.controls.push(control)
        this.detail.controls.push(control)
      },
      deleteControl(control){
        
      //   if(this.controls.length>1){
      //     for(var i=0;i<this.controls.length;i++){
      //     if(this.controls[i].id==control.id){
      //       this.controls.splice(i,1)
      //     }
      //   }
      // }
      if(this.detail.controls.length>1){
          for(var i=0;i<this.detail.controls.length;i++){
          if(this.detail.controls[i].id==control.id){
            this.detail.controls.splice(i,1)
          }
        }
      }
    },
      // addDetail() {
      //   this.$emit("addDetail",this.item)
      // },
      reduceDetail(){
        
        this.$emit("reduceDetail",this.item,this.model)
        // console.log("reduceDetail")
      },
      add () {
        
        this.edit(this.modelDefault);
      },
      edit (record) {
        
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            this.model.templateId=this.item.id
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },

      popupCallback(value,row){
      debugger
         this.model = Object.assign(this.model, row);
      },
    }
  }
</script>